package com.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.entity.Leader;
import com.ssm.entity.Organization;
import com.ssm.service.LeaderService;
import com.ssm.service.OrganizationService;
import com.ssm.util.JSONResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/leader")
public class LeaderController {

    @Resource
    private OrganizationService organizationService;

    @Resource
    private LeaderService leaderService;

    @RequestMapping("/listOrg")
    public  JSONResult listOrg(Organization organization){
        List<Organization> list=organizationService.listOrganization(organization);
        return JSONResult.success(list);
    }

    @RequestMapping("/add")
    public JSONResult addLeader(Leader leader){
        leaderService.addLeader(leader);
        return JSONResult.success();
    }

    @RequestMapping("/update")
    public JSONResult updateLeader(Leader leader){
        leaderService.updateLeader(leader);
        return JSONResult.success();
    }

    /**
     * 数据单查
     */
    @RequestMapping("/get/{id}")
    public JSONResult getLeader(@PathVariable("id") Integer id){
        return JSONResult.success(leaderService.getLeader(id));
    }

    @RequestMapping("/removeBatch")
    public JSONResult removeBatchLeader(@RequestParam("ids[]") Integer[] ids){
        leaderService.removeBatchLeader(ids);
        return JSONResult.success();
    }

    @RequestMapping("/list")
    public JSONResult listLeader(@RequestParam(value = "pageNumber",required = false,defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
                                       Leader leader){
        //使用分页助手
        PageHelper.startPage(pageNum, pageSize);
        //查询当前页面的记录信息
        List<Leader> list =  leaderService.listLeader(leader);
        //初始化分页信息对象，并将当前页面的记录信息存入该对象
        PageInfo<Leader> info = new PageInfo<>(list);
        return JSONResult.success(list, info.getTotal());

    }
    @RequestMapping("/username")
    public JSONResult name(String name){
        int exists = leaderService.selectLeaderByname(name);
        if(exists==0){
            return JSONResult.success();
        }
        return JSONResult.error();
    }
}
